package com.peanut.devlibrary.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton;


public class TabRadioButton extends RadioButton {

	private Drawable drawable = null;
	
	private boolean 	mShowNoti = false;
	private int 		mNotiSize;
	private Paint mNotiPaint;

	private int 		mDrawableSize;
	
	public TabRadioButton(Context context, AttributeSet attrs,
                          int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public TabRadioButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public TabRadioButton(Context context) {
		super(context);
		init();
	}
	
	private void init(){
		if(getCompoundDrawables()[1] != null){

		}
		mNotiSize = dip2px(getContext(), 4);
		mDrawableSize = dip2px(getContext(), 30);
		drawable =  getCompoundDrawables()[1];
		if(drawable != null){
			drawable.setBounds(0, 0, mDrawableSize, mDrawableSize);
			setCompoundDrawables(null, drawable, null, null);
			setCompoundDrawablePadding(dip2px(getContext(), 1));
			invalidate();
		}
	
	}
	
	@Override
	public void setChecked(boolean checked) {
		// TODO Auto-generated method stub
		super.setChecked(checked);
	}

	
	 public int dip2px(Context context, float dpValue) {
	        final float scale = context.getResources().getDisplayMetrics().density;
	        return (int) (dpValue * scale + 0.5f);
	    }

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		
		if(mShowNoti){
			//画个小红点
			canvas.drawCircle(getWidth() / 2 + mDrawableSize / 2,mNotiSize + dip2px(getContext(), 3), mNotiSize, getNotiPaint());
		}
	}
	
	/**
	 * 
	 * 显示红点的方法
	 * @param isVisiable
	 */
	public void setNotifyVisiable(boolean isVisiable){
		this.mShowNoti = isVisiable;
		invalidate();
	}
	
	private Paint getNotiPaint(){
		if(mNotiPaint == null){
			mNotiPaint = new Paint();
			mNotiPaint.setColor(Color.RED);
			mNotiPaint.setAntiAlias(true);
			mNotiPaint.setStyle(Style.FILL);
		}
		return mNotiPaint;
	}
}
